//
//  FirstLoadViewController.m
//  ScrollView
//
//  Created by Shin Stupid on 12/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FirstLoadViewController.h"
#import "CustomCell.h"
@implementation FirstLoadViewController
@synthesize listData;
@synthesize mscrollview;
@synthesize lastIndexPath;

static FirstLoadViewController *FirstViewControllerInstance=nil;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //self.title = GET_STRING(@"First", APP_DELEGATE.langId);
        self.tabBarItem.image = [UIImage imageNamed:@"tabbar_setting.png"];
        self.lastIndexPath=nil;
        FirstViewControllerInstance=self;
    }
    return self;
}
+(FirstLoadViewController*)getFirstViewControllerInstance
{
    return FirstViewControllerInstance;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - init UITABLEVIEW
-(void)initTableView
{
    ///myTable=
    UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height)
                                                   style:UITableViewStyleGrouped];
    
    myTable = tv;
    
    [mscrollview addSubview:myTable]; 
    [myTable setBackgroundColor:[UIColor clearColor]];
    myTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    myTable.delegate=self;
    myTable.scrollEnabled = NO;
    myTable.dataSource = self;
    [myTable reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSArray *array = [[NSArray alloc] initWithObjects:@"Vietnamese",@"English",nil];
    self.listData = array;
    SAFE_RELEASE(array);
    
    [self initTableView];
    
    
   

    [mscrollview setBackgroundColor:[UIColor clearColor]];
    if (IS_IPHONE5) {
        
    }
    else
    {
        mscrollview.frame = CGRectMake(0, 0, 320, 460);
        mscrollview.contentSize = CGSizeMake(320, 500-80);
    }
   // UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    //BOOL isCurrentlyLandscapeView = UIDeviceOrientationIsLandscape(deviceOrientation);
    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.listData count];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
       
        [self changeTheViewToPortrait:YES andDuration:duration];
        
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        
       [self changeTheViewToPortrait:NO andDuration:duration];
    }
}


- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
   
   
    if(portrait){
    
      
      mscrollview.contentSize = CGSizeMake(320, 500-80);
        
    }
    else{   
       
        
        
       mscrollview.contentSize = CGSizeMake(320, 500-180);
    }
    
    [UIView commitAnimations];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];//[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CheckMarkCellIdentifier] autorelease];//CellIdentifier
    }
    NSUInteger row = [indexPath row];
    BOOL isEL=FALSE;
    if([APP_DELEGATE.langId isEqualToString:@"eng"])
    {
        isEL=TRUE;
    }
    int iRow = [indexPath row];
    if(isEL==TRUE)
    {
        /// is EL
        if(row==1)
        {
            cell.accessoryType =UITableViewCellAccessoryCheckmark;            
            lastIndexPath=indexPath;
        }
        else
        {
            cell.accessoryType =UITableViewCellAccessoryNone; 
        }
    }
    else
    {
        if(row==0)
        {
            cell.accessoryType =UITableViewCellAccessoryCheckmark;
            lastIndexPath=indexPath;
        }
        else
        {
            cell.accessoryType =UITableViewCellAccessoryNone; 
        }
    }
    //cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    // Set up the cell…  
    cell.textLabel.text = [listData objectAtIndex:iRow]; 
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    int row = [indexPath row];
    
    if(row==0)
    {
        APP_DELEGATE.langId=@"vn";
    }
    else
    {
        APP_DELEGATE.langId=@"eng";
    }
    [tableView reloadData];
   
  //  [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)dealloc
{
    [super dealloc];
    SAFE_RELEASE(listData);
}
@end
